<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2" style="padding-left:15px;padding-top:5px">
			<label for="phone_search" class="control-label">手机号</label>
		</div>
		<div class="col-sm-3">
			<input id="phone_search" class="form-control"></input>
		</div>
		<div class="col-sm-3" style="padding-left:90px;padding-top:5px">
			<label for="status_search" class="control-label">状态：</label>
		</div>
		<div class="col-sm-3">
      <select class="form-control" id="status_search" name="status_search">
	      <option value="" selected="selected">全部</option>
				<option value="0">正常</option>
				<option value="1">禁用</option>
			</select>
    </div>
	</div>
	<div class="col-sm-12">
		<br>
	<table id="user_list_table" class="table table-hover table-bordered table-striped table-striped">
		<thead>
			<tr>
				<td>编号</td>
				<td>手机号</td>
				<td>注册时间</td>
				<td>积分</td>
				<td>昵称</td>
				<td>状态</td>
				<td>status</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>

		</tbody>
	</table>
</div>
</div>

<include file='Index/footer' />
<include file='User/user-detail' />
<include file='User/user-credit' />
<script>
$(document).ready(function() {
		loadTable();
});
var table;
function loadTable(){
	table = $('#user_list_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/User/getUser/")}',
		"columns": [
					{ "data": 'id' },
					{ "data": 'phone' },
	        { "data": 'createtime' },
	        { "data": 'credit' },
	        { "data": 'name' },
	        { "data": 'status' },
	        { "data": 'status' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
			"targets": 0,
			"render": function (data, type, row, table) {
        return table.row + 1;
      }
    },
    {
			"targets": 5,
			"render": function (data, type, row, table) {
				switch(data){
					case '0':
					 data = '正常';
					 break;
					case '1':
						data = '已禁用';
						break;
				}
        return data;
      }
    },
    {
			"targets": 6,
			"visible":false
    },
    {
			"targets": -1,
			"render": function (data, type, row, table) {
				var status, btn;
				switch(row.status){
					case '0':
						btn = '禁用';
						status = '1';
						break;
					case '1':
						btn = '恢复';
						status = '0';
						break;
				}
				return '<button class="btn btn-info" onclick="view(\''+row.user_id+'\')">查看</button> <button class="btn btn-primary" onclick="forbid(\''+row.user_id+'\', \''+status+'\')">'+btn+'</button> <button class="btn btn-success" onclick="del(\''+row.user_id+'\')">删除</button> <button class="btn btn-warning" onclick="modCredit(\''+row.user_id+'\')">修改积分</button>';
      }
    }]
	});

	$("#phone_search").keyup(function(){
		table.column(1).search(this.value).draw();
  });

	$("#status_search").on( 'change', function () {
 		table.column(6).search(this.value).draw();
  });
}

function view(user_id){
	$.ajax({
			url: '{:U("Admin/User/getUser/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"user_id":user_id
			}
		})
		.success(function(data){
			$('#phone').html(data.phone);
			$('#createtime').html(data.createtime);
			$('#credit').html(data.credit);
			$('#name').html(data.name);
			
			switch(data.status){
				case '0':
					$('#status').html("正常");
					break;
				case '1':
					$('#status').html("已禁用");
					break;
			}

			$('#user_detail_modal').modal({
				backdrop : "static"
			});
	});
}

function forbid(user_id, status){
	$.ajax({
		url: '{:U("Admin/User/updateUser/")}',
		type: 'post',
		dataType: 'json',
		data:{
			"user_id":user_id,
			"status":status
		}
	})
	.success(function(data){
		location.reload();
	});
}

function del(user_id){
	if(!confirm("是否删除该用户？（状态不可逆）")){
		return;
	}
	$.ajax({
		url: '{:U("Admin/User/deleteUser/")}',
		type: 'post',
		dataType: 'json',
		data:{
			"user_id":user_id
		}
	})
	.success(function(data){
		location.reload();
	});
}
var user_id = '';
function modCredit(user_id){
	window.user_id = user_id;
	$('#user_credit_modal').modal({
		backdrop : "static"
	});
}
</script>
